package cn.itsource.pethome.quartz.job;

import cn.itsource.pethome.order.domain.ProductOrder;
import cn.itsource.pethome.order.mapper.ProductOrderMapper;
import cn.itsource.pethome.order.service.IProductOrderService;
import cn.itsource.pethome.pay.constant.PayConstants;
import cn.itsource.pethome.pay.domain.PayBill;
import cn.itsource.pethome.pay.mapper.PayBillMapper;
import cn.itsource.pethome.quartz.domain.QuartzJobInfo;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

public class QuartzJob implements Job {
    @Autowired
    private ProductOrderMapper productOrderMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
        //获取传入参数params 是一个QuartzJobInfo对象
        Object params = mergedJobDataMap.get("params");
        //转换
        QuartzJobInfo quartzJobInfo = (QuartzJobInfo)params;
        //获取订单单号
        String oderSn = (String) quartzJobInfo.getParams().get("params");
        //判断订单类型
        switch (quartzJobInfo.getType()){
            //如果是服务订单
            case PayConstants.BUSINESSTYPE_PRODUCT:
                //通过订单单号查询服务订单
                ProductOrder productOrder = productOrderMapper.loadByOderSn(oderSn);
                //将订单状态设为取消
                productOrder.setState(-1);
                //保存订单
                productOrderMapper.update(productOrder);
                //改变支付订单
                PayBill payBill = payBillMapper.loadByorderSn(oderSn);
                payBill.setState(-1);
                payBill.setUpdateTime(new Date());
                payBillMapper.update(payBill);
                break;
            case PayConstants.BUSINESSTYPE_ADOPT:
                break;
        }


    }
}
